﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cua_Hang.DTO;
using Cua_Hang.BUS;
using System.Data;
namespace Cua_Hang
{
    public partial class DangNhap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

      //-----------------------
        //============= chuyen List thanh DataTable===========================
        //
        public DataTable ConvertListDangNhapDTOToDataTable(List<DangNhap_DTO> lst)
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("ID_NhanVien");
            dt.Columns.Add("ID_cuahang");
            dt.Columns.Add("user_nhanvien");
            dt.Columns.Add("pass_nhanvien");
            for (int i = 0; i < lst.Count; i++)
            {
                dt.Rows.Add(
                        lst[i].ID_NhanVien,
                        lst[i].ID_cuahang,
                        lst[i].User_nhanvien,
                        lst[i].Pass_nhanvien

                    );
            }
            return dt;
        }
        //============= ket thuc chuyen List thanh DataTable=======================
        public DangNhap_DTO HienLaiTextBox(DangNhap_DTO dn)
        {
            DangNhap_DTO dm = new DangNhap_DTO();
            List<DangNhap_DTO> lst = DangNhap_BUS.Load_All_NhanVien_BUS(dn);
            DataTable dt = new DataTable();
            dt = ConvertListDangNhapDTOToDataTable(lst);
            foreach (DataRow dr in dt.Rows)
            {
                //dm.ID_cuahang = (int)dr["ID_cuahang"];
                dm.ID_cuahang = Convert.ToInt32(dr["ID_cuahang"].ToString());

            }
            return dm;
        }
      //------------------------
        protected void BT_dangnhap_Click(object sender, EventArgs e)
        {
            DangNhap_DTO dn = new DangNhap_DTO();
            dn.Pass_nhanvien= TB_pass.Text.ToString();
            dn.User_nhanvien= TB_user.Text.ToString();
            if (DangNhap_BUS.KiemTra_DangNhap_BUS(dn) == true)
            {
                //DangNhap_DTO dn = new DangNhap_DTO();
                dn.Pass_nhanvien = TB_pass.Text.ToString();
                dn.User_nhanvien = TB_user.Text.ToString();
                DangNhap_DTO dnn = new DangNhap_DTO();
               dnn= HienLaiTextBox(dn);
                dn.User_nhanvien = TB_user.Text.Trim();
                Session["Login"] = dnn.ID_cuahang.ToString();
                Response.Redirect("http://localhost:1482/Admin/Default.aspx");
                
            }
            else
            {
                Response.Redirect("DangNhap.aspx");
            }
        }
    }
}